package com.taikang.hmp.hbusiness.diabetes.measure.sugar.action.view;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.Btools;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.bean.SugarRecord;
import com.taikang.hmp.hbusiness.diabetes.measure.sugar.service.ISugarService;

/**
 * 血糖图表 author:wyf
 */
public class SugarViewAction extends BaseAction {
	private static Logger logger = LoggerFactory.getLogger(SugarViewAction.class);
	
	ISugarService sugarService;


	public ISugarService getSugarService() {
		return sugarService;
	}

	public void setSugarService(ISugarService sugarService) {
		this.sugarService = sugarService;
	}

	public String executeAction() {
		// 显示血糖折线图
		if ("showSugarChart".equals(requestDto.getMethod())) {
			return showSugarChart();
			// 显示血糖表
		} else if ("showSugarTable".equals(requestDto.getMethod())) {
			return showSugarTable();
			// 显示血糖月报告
		} else if ("showSugarReport".equals(requestDto.getMethod())) {
			return showSugarReport();
		} else {
			throw new TKException("no support method:" + requestDto.getMethod());
		}
	}

	/**
	 * 显示血糖月报告
	 * 
	 * @return
	 */
	public String showSugarReport()  {
		ResponseDto responseDto = new ResponseDto();
		
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		double first = 0;
		double after = 0;
		double sleep = 0;
		
		
		
		List<SugarRecord> recordsfirst = sugarService.getSugarFirst(paramDto);
		List<SugarRecord> recordsafter = sugarService.getSugarAfter(paramDto);
		List<SugarRecord> recordssleep = sugarService.getSugarSleep(paramDto);
		first = this.avgSugar(recordsfirst);
		after = this.avgSugar(recordsafter);
		sleep = this.avgSugar(recordssleep);
		
		String desc = "本月您的血糖维持较为平稳，空腹血糖均值在"+Btools.formatDouble(first)+"mmol/L，餐后血糖均值在"+Btools.formatDouble(after)+" mmol/L，睡前血糖均值在"+Btools.formatDouble(sleep)+" mmol/L。";
		String subject = "空腹血糖、睡前血糖还是有些偏高，请您要再多加控制，调整血糖！";
		Dto dto = new BaseDto();
		dto.put("recordsfirst", recordsfirst);
		dto.put("recordsafter", recordsafter);
		dto.put("recordssleep", recordssleep);
		dto.put("first", Btools.formatDouble(first));
		dto.put("after", Btools.formatDouble(after));
		dto.put("sleep", Btools.formatDouble(sleep));
		dto.put("desc", desc);
		dto.put("subject", subject);
		
		responseDto.pushResultDto(dto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 显示血糖折线图
	 * 
	 * @return
	 */
	public String showSugarChart()  {
		ResponseDto responseDto = new ResponseDto();
		
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		List<SugarRecord> records = sugarService.getSugarChart(paramDto);
		Dto dto = new BaseDto();
		dto.put("records", records);
		responseDto.pushResultDto(dto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	/**
	 * 显示血糖表
	 * 
	 * @return
	 */
	public String showSugarTable()  {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);

		List<SugarRecord> records = sugarService.getSugarTable(paramDto);
		Dto dto = new BaseDto();
		dto.put("records", records);
		responseDto.pushResultDto(dto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	private double avgSugar(List<SugarRecord> records){
		double result = 0;
		int size = records.size();
		for(int i=0;i<records.size();i++){
			SugarRecord temp = records.get(i);
			try{
			result = result + Double.parseDouble(temp.getValue());
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(size>0){
			result = result/size;
		}
		
		return result;
	}
}
